<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/9/13  15:18
 */
namespace app\api\logic;
use app\api\BaseModel;
use app\api\controller\Base;
use think\Cache;
use think\Db;
use think\Loader;
use Webimsdk\take\TimRestAPI;

class Test extends BaseModel
{
    //检测参数是否已传
//    protected $ParameterDetection  = [
//        'accountImport'=>['identifier','nick','face_url'],
//    ];
    #app基本信息
    protected $sdkappid;
    protected $usersig;
    protected $identifier;

    protected $expiry_after;
    protected $protected_key_path;
    protected $tool_path;
    protected $api;

   public function __construct(){
       parent::__construct();
       $this->sdkappid = 1400138386;
       $this->usersig = '';
       
       $this->protected_key_path='../extend/webimsdk/key/private_key';
       $this->tool_path='../extend/webimsdk/signature/windows-signature64.exe';
       Loader::import('webimsdk\TimRestApi', EXTEND_PATH);
       $this->identifier = 'admin1';
       $this->api= new TimRestAPI();
       $this->api->init($this->sdkappid, $this->identifier);

       //获取签名访问别的接口
       $this->api->generate_user_sig($this->identifier, '60',$this->protected_key_path, $this->tool_path);
   }


    public function index(){
        $data = [];
        if(!isset($this->requestData['identifier']))return ['请传用户名生成签名!'];
        $this->identifier = $this->requestData['identifier'];
        $sig = $this->api->generate_user_sig($this->identifier, '604800',$this->protected_key_path, $this->tool_path);
        if(empty($sig)){
            return ['message'=>'生成签名失败!'];
        }
        $data['sig']=$sig[0];
        $data['identifier']=$this->identifier;
        return $data;
    }

    public function accountImport($identifier, $nick, $face_url){
//        $identifier = $this->requestData['identifier'];
//        $nick=$this->requestData['nick'];
//        $face_url=$this->requestData['face_url'];
        if(empty($identifier))return ['message'=>'indentifier参数必传!'];
        $result = $this->api->account_import($identifier, $nick, $face_url);
        return $result;
//        $data = [];
//        if($result['ActionStatus'] != 'OK')return ['message'=>'添加失败!'];
//        var_dump($result);
//        die('123');
    }


    public function updateAccountIm()
    {
        $data = db('member')->where(['im_username'=>''])->field('id')->select();
        if(empty($data))return ['message'=>'没有需要导入的用户!'];
        foreach($data as $key=>$value){
            $account = db('member')->where(['id'=>$data[$key]['id']])->find();
            $nick = $account['nickname'];
            $face_url = $account['headimage'];
            $identifier= $account['id'].'im'.time();
            $restIm = $this->accountImport($identifier, $nick, $face_url);
            if($restIm['ActionStatus'] != 'OK')return ['message'=>'添加失败!'];
            $dataIm['im_username'] =$identifier;
            $upRest = db('member')->where(['id'=>$data[$key]['id']])->update($dataIm);
            if(!$upRest)return ['message'=>'添加失败!'];
            sleep(1000);
        }

    }

}